<?php
include_once FILE_CLASS_SJABLOON;
include_once FILE_CLASS_TEXTOBJECT;

//$recht_bitfield = new RechtBitfield();
//$recht_bitfield->setBitfield($data_array_bitfield);
//$recht_array = array('13','14');
//
//if ($recht_bitfield->heeftRechten($recht_array) == false) {
//
// echo '<script type="text/javascript">      
//           alert(\'Geen Rechten!\');
//           window.location = "index.php";
//           </script>';
//            exit;
//}else{

$versie_nr = '';
$sjabloon_class = new Sjabloon();

if (isset($_GET['versie_nr'])) {
    if (is_numeric($_GET['versie_nr'])) {
        $versie_nr = $_GET['versie_nr'];
        $sjabloon = $sjabloon_class->getSjabloon($versie_nr);
    }
} else {
    echo 'Ongeldig versienummer!';
}

////$rechten_array = array(RIGHT_ADMIN_ADD, RIGHT_ADMIN_DEL_USER);
//
//$recht = new RechtBitfield();
//$recht->addRecht($rechten_array);
//                    $rechten_toevoegen = array(RIGHT_ADMIN_ADD, RIGHT_ADMIN_DEL_USER);
//                
//                    if($recht->heeftRechten($rechten_toevoegen)){
//                } else { 
//                    echo 'error';
//                }
?>
<link rel="stylesheet" href="../css/stylesheet.css" type="text/css" />
<script src="js/sjabloon_aanpassen.js" >
</script>
<div id="header">
    <div id="logo"></div>
    <div id="headertext">Sjablonen VGP</div>
</div>
<div id="vgp_sjabloon_menu">
    <center><h3><span style="color:#FFFFFF;">Toevoegen</span></h3></center>
    <hr />
    <table>
        <tr>
            <td><span style="color:#FFFFFF;">Onder regel:</span></td><td><a href="#" class="tooltip"><img src="images/tooltip_icon.png"><span>Klik op een tabel cel om het nummer onderin toe te voegen</span></a></td>
        </tr>
        <tr>
            <td><input type="text" id="row_field" name="row_field" readonly style="width:100px;"/><br/><br/></td>
        </tr>
        <tr>
            <td><span style="color:#FFFFFF; font-size:18px;">Onderdelen:</span></td><td></td>
        </tr>
        <tr>
            <td><span style="color:#FFFFFF; font-size:14px;">Risicoaandachtsgebied</span></td><td><span id="rag_plus" style="font-weight: bold; color:#DEE463;">+</span></td>
        </tr>
        <tr>
            <td><span style="color:#FFFFFF; font-size:14px;">Risicogebied</span></td><td><span id="rg_plus" style="font-weight: bold; color:#DEE463;">+</span></td>
        </tr>
        <tr>
            <td><span style="color:#FFFFFF; font-size:14px;">Risico</span></td><td><span id="risico_plus" style="font-weight: bold; color:#DEE463;">+</span></td>
        </tr>
        <tr>
            <td><span style="color:#FFFFFF; font-size:14px;">Maatregel</span></td><td><span id="pm_plus" style="font-weight: bold; color:#DEE463;">+</span></td>
        </tr>
    </table>
</div>
<div id="content2">
    <div id="contentbg">
        <div id="contentblock1">
            <div id="contentblock1text">
                <form action="index.php?page=sjabloon_verwerken&status=draft" method="post">
                    <span style="font-size: 16px;">Sjabloon beschrijving: </span><input type="text" name="beschrijving" />
                    <table border="1" width="590px" style="background-color:#FFFFFF; border-color: #C6C6C6 ;" id="sjabloon">
                        <?php
                        $rag_index = 0;
                        $rag_count = 0;
                        $rag = count($sjabloon);
                        $rg_index = 0;

                                    echo '<th style="background-color: #D0CF22; color: #000000; font-size:16px;">' . VGP_SJABLOON_TEKST_RG . '</th><th style="background-color: #D0CF22; color: #000000; font-size:16px;">' . VGP_SJABLOON_TEKST_RISICO . '</th><th style="background-color: #D0CF22; color: #000000; font-size:16px;">' . VGP_SJABLOON_TEKST_PM . '</th>';
//Sjabloon array

                        foreach ($sjabloon as $idx => $ragv_array) {
                            if (!is_array($ragv_array)) {
                                continue;
                            }
                            //Risicoaandachtsgebied versie array
                            foreach ($ragv_array as $idx2 => $rag_array) {
                                //Skip risicoaandachtsgebied versie
                                if (strcmp($idx2, 'risicoaandachtsgebied_versie') == 0)
                                    continue;
                                if (!is_array($rag_array)) {
                                    continue;
                                }
                                $rg = count($rag_array);
                                $rag_count = 0;
                                $rg_count = 0;
                                //risicoaandachtsgebied array
                                foreach ($rag_array as $idx3 => $rag_array_output) {
                                    if (array_key_exists('risico_aandachtsgebied_naam', $rag_array_output)) {
                                        $rag_count++;
                                        echo '<tr style="background-color: #EDE9E4; color: #000000; font-size:16px ;" name=""><td class="vgp_menu" colspan="3">Risico&#39s van <input type="text" value="' . $rag_array_output['risico_aandachtsgebied_naam'] . '" name="risicoaandachtsgebied_' . $rag_index . '"/>';
                                        if ($rag == $rag_count) {
                                            //echo '<span id="rag_plus" style="font-weight: bold; color:#3DB1ED;">+</span>';
                                        }
                                        echo '</td></tr>';

                                        
                                        continue;
                                    }

                                    if (!is_array($rag_array_output)) {

                                        continue;
                                    }

                                    //risicogebied versie array
                                    foreach ($rag_array_output as $idx4 => $rgv_array) {
                                        
                                        $rg = count($rag_array_output);
                                        
                                        // Skip risicogebied versie
                                        if (strcmp($idx4, 'risicogebied_versie') == 0)
                                            continue;
                                        if (!is_array($rgv_array)) {
                                            continue;
                                        }

                                        $risico_count = 0;
                                        
                                        $risico = count($rgv_array)-1;
                                        //risicogebied array                                  
                                        foreach ($rgv_array as $idx5 => $rg_array) {
                                            
                                            if (array_key_exists('risico_gebied_naam', $rg_array)) {
                                                $rg_count++;
                                                echo '<tr style="background-color: #FFFFFF; color: #000000; font-size:14px" id="rg_row"><td class="vgp_menu"><input type="text" name="risicogebied_' . $rag_index . '_' . $rg_index . '" style="width:93%;" value="' . $rg_array['risico_gebied_naam'] . '" />';
                                                if ($rg_count == $rg) {
                                                   // echo'<span onclick="add_rg_row()" style="font-weight: bold; color:#3DB1ED;">+</span>';
                                                }
                                                echo '</td>';

                                                
                                                $risico_index = 0;
                                                continue;
                                            }
                                            
                                            if (!is_array($rg_array)) {
                                                continue;
                                            }

                                            foreach ($rg_array as $idx5b => $rg_array_output) {
                                                if (strcmp($idx5b, 'risico_versie') == 0)
                                                    continue;
                                                if (strcmp($idx5b, '$rg_array_output') == 0)
                                                    continue;

                                                if (!is_array($rg_array_output)) {
                                                    continue;
                                                }
                                                
                                                
                                                //risico versie array
                                                foreach ($rg_array_output as $idx6 => $rv_array) {
                                                    
                                                    if (strcmp($idx6, 'risico_versie') == 0)
                                                        continue;
                                                    if (!is_array($rv_array)) {
                                                        continue;
                                                    }
                                                    $pmva_count = 0;
                                                    $pmva = count($rv_array)-1;
                                                    //Skip risico versie
                                                    if (array_key_exists('risico_naam', $rv_array)) {
                                                        $risico_count++;
                                                        if ($risico_index != '0'){
                                                            echo '<td class="vgp_menu">&nbsp;</td>';
                                                        }
                                                        
                                                        echo '<td class="vgp_menu" style="background-color: #FFFFFF; color: #000000; font-size:14px" ><input type="text" name="risico_' . $rag_index . '_' . $rg_index . '_' . $risico_index . '" style="width:93%;" value="' . $rv_array['risico_naam'] . '" />';

                                                        if ($risico_count == $risico) {
                                                           // echo '<span onclick="add_risico_row()" style="font-weight: bold; color:#3DB1ED;">+</span>';
                                                        }

                                                        echo '</td>';

                                                        $maatregel_index = 0;
                                                        continue;
                                                    }




                                                    //risico array
                                                    foreach ($rv_array as $idx7 => $r_array) {
                                                         
                                                        if (strcmp($idx7, 'preventieve_maatregel_versie') == 0) {
                                                                continue;
                                                            } 
                                                        if (!is_array($r_array)) {
                                                            continue;
                                                        }

                                                        //maatregel versie array
                                                        $lvl8_cnt = 0;

                                                        foreach ($r_array as $idx8 => $r_array_output) {
                                                            
                                                            if (strcmp($idx8, 'preventieve_maatregel_versie') == 0) {
                                                                continue;
                                                            }
                                                            if (!is_array($r_array_output)) {
                                                                continue;
                                                            }

                                                            //Preventieve maatregel versie array
                                                            //Deze moet hier blijven staan omdat hierboven dingen worden overgeslagen.
                                                            //Als deze verplaatst wordt, worden er meerdere verschillende resultaten teruggegeven
                                                            
                                                            $pmva_count++;
                                                            if ($maatregel_index != '0') {
                                                                echo '<td class="vgp_menu">&nbsp;</td><td class="vgp_menu">&nbsp;</td>';
                                                            }

                                                            echo '<td class="vgp_menu" style="background-color: #FFFFFF; color: #000000; font-size:14px"><input name="maatregel_' . $rag_index . '_' . $rg_index . '_' . $risico_index . '_' . $maatregel_index . '" type="text" style="width:93%;" value="' . $r_array_output['preventieve_maatregel_naam'] . '" />';
                                                            
                                                            if ($pmva_count == $pmva) {
                                                                //echo '<span id="pm_plus" style="font-weight: bold; color:#3DB1ED;">+</span>';
                                                            }

                                                            echo '</td></tr>';
                                                            $maatregel_index++;
                                                        }
                                                    }
                                                    $risico_index++;
                                                }
                                            }
                                        }
                                        $rg_index++;
                                    }
                                }
                            }
                            $rag_index++;
                        }
                        ?>

                    </table>
                    <p>
                        <span style="font-size: 16px;">Aanpassingen:</span><br/>
                        <textarea rows="4" name="aanpassingen">Typ hier de gemaakte wijzigingen</textarea>
                    </p>
                    <p>
                        <a style="float: left;" id="koppelknoppen" href="index.php?page=overzicht_sjabloonlijst">
                            Terug
                        </a>
                        <input type="submit" style="float: right;" id="koppelknoppen" value="Opslaan als nieuw sjabloon" name="sjabloon">
                        <input type="submit" style="float: right;" id="koppelknoppen" value="Opslaan als draft" name="draft">
                        <a style="float: right;" id="koppelknoppen" href="index.php?page=sjabloon_verwerken&status=preview">
                            Opslaan als preview
                        </a>
                    </p>
                </form>
            </div>
        </div>
    </div>        
</div>
<?php
//}
?>
